package core.org.akaza.openclinica.service.extract;

import java.io.File;
import java.text.SimpleDateFormat;

import core.org.akaza.openclinica.bean.extract.DatasetBean;
import core.org.akaza.openclinica.bean.extract.ExtractPropertyBean;

public class ExtractUtils {

    /**
     * Returns the datetime based on pattern :"yyyy-MM-dd-HHmmssSSS", typically for resolving file name
     * @param endFilePath
     * @param dsBean
     * @param filePath
     * @return
     */
    public String resolveVars(String endFilePath, DatasetBean dsBean, String filePath) {

        if (endFilePath.contains("$exportFilePath")) {
            endFilePath = endFilePath.replace("$exportFilePath", filePath + "datasets");// was + File.separator, tbh
        }

        if (endFilePath.contains("${exportFilePath}")) {
            endFilePath = endFilePath.replace("${exportFilePath}", filePath + "datasets");// was + File.separator, tbh
        }
        if (endFilePath.contains("$datasetId")) {
            endFilePath = endFilePath.replace("$datasetId", dsBean.getId() + "");
        }
        if (endFilePath.contains("${datasetId}")) {
            endFilePath = endFilePath.replace("${datasetId}", dsBean.getId() + "");
        }
        if (endFilePath.contains("$datasetName")) {
            endFilePath = endFilePath.replace("$datasetName", dsBean.getName());
        }
        if (endFilePath.contains("${datasetName}")) {
            endFilePath = endFilePath.replace("${datasetName}", dsBean.getName());
        }

        if (endFilePath.contains("$dateTime")) {
            String simpleDatePattern = "yyyy-MM-dd-HHmmssSSS";
            SimpleDateFormat sdfDir = new SimpleDateFormat(simpleDatePattern);
            endFilePath = endFilePath.replace("$dateTime", sdfDir.format(new java.util.Date()));
        }
        if (endFilePath.contains("${dateTime}")) {
            String simpleDatePattern = "yyyy-MM-dd-HHmmssSSS";
            SimpleDateFormat sdfDir = new SimpleDateFormat(simpleDatePattern);
            endFilePath = endFilePath.replace("${dateTime}", sdfDir.format(new java.util.Date()));
        }
        if (endFilePath.contains("$date")) {
            String dateFilePattern = "yyyy-MM-dd";
            SimpleDateFormat sdfDir = new SimpleDateFormat(dateFilePattern);
            endFilePath = endFilePath.replace("$date", sdfDir.format(new java.util.Date()));
        }
        if (endFilePath.contains("${date}")) {
            String dateFilePattern = "yyyy-MM-dd";
            SimpleDateFormat sdfDir = new SimpleDateFormat(dateFilePattern);
            endFilePath = endFilePath.replace("${date}", sdfDir.format(new java.util.Date()));
        }
        //TODO change to dateTime

        return endFilePath;
    }

    //TODO: ${linkURL} needs to be added

    /**
     * for dateTimePattern, the directory structure is created. "yyyy" + File.separator + "MM" + File.separator + "dd" + File.separator,
     * to resolve location
     */
    public String getEndFilePath(String endFilePath, DatasetBean dsBean, SimpleDateFormat sdfDir, String filePath) {
        String simpleDatePattern = "yyyy" + File.separator + "MM" + File.separator + "dd" + File.separator;
        SimpleDateFormat sdpDir = new SimpleDateFormat(simpleDatePattern);

        String datePattern = "yyyy-MM-dd";
        SimpleDateFormat dateDir = new SimpleDateFormat(datePattern);

        if (endFilePath.contains("$exportFilePath")) {
            endFilePath = endFilePath.replace("$exportFilePath", filePath + "datasets");// was + File.separator, tbh
        }

        if (endFilePath.contains("${exportFilePath}")) {
            endFilePath = endFilePath.replace("${exportFilePath}", filePath + "datasets");// was + File.separator, tbh
        }
        if (endFilePath.contains("$datasetId")) {
            endFilePath = endFilePath.replace("$datasetId", dsBean.getId() + "");
        }
        if (endFilePath.contains("${datasetId}")) {
            endFilePath = endFilePath.replace("${datasetId}", dsBean.getId() + "");
        }
        if (endFilePath.contains("$datasetName")) {
            endFilePath = endFilePath.replace("$datasetName", dsBean.getName());
        }
        if (endFilePath.contains("${datasetName}")) {
            endFilePath = endFilePath.replace("${datasetName}", dsBean.getName());
        }

        if (endFilePath.contains("$dateTime")) {
            endFilePath = endFilePath.replace("$dateTime", sdfDir.format(new java.util.Date()));
        }
        if (endFilePath.contains("${dateTime}")) {
            endFilePath = endFilePath.replace("${dateTime}", sdfDir.format(new java.util.Date()));
        }
        if (endFilePath.contains("$date")) {

            endFilePath = endFilePath.replace("$date", dateDir.format(new java.util.Date()));
        }
        if (endFilePath.contains("${date}")) {
            endFilePath = endFilePath.replace("${date}", dateDir.format(new java.util.Date()));
        }

        return endFilePath;
    }

    public ExtractPropertyBean setAllProps(ExtractPropertyBean epBean, DatasetBean dsBean, String filePath) {
        epBean.setFiledescription(resolveVars(epBean.getFiledescription(), dsBean, filePath));
        epBean.setLinkText(resolveVars(epBean.getLinkText(), dsBean, filePath));
        epBean.setHelpText(resolveVars(epBean.getHelpText(), dsBean, filePath));
        epBean.setFileLocation(resolveVars(epBean.getFileLocation(), dsBean, filePath));
        epBean.setFailureMessage(resolveVars(epBean.getFailureMessage(), dsBean, filePath));
        epBean.setSuccessMessage(resolveVars(epBean.getSuccessMessage(), dsBean, filePath));
        epBean.setZipName(resolveVars(epBean.getZipName(), dsBean, filePath));
        epBean.setDatasetName(dsBean.getName());//JN:Adding this line to reflect the dataset name in the scheduled jobs list.
        return epBean;
    }

}
